package com.elves.auth.service;

import com.elves.auth.util.RSACoder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.util.Map;

@Component
@Slf4j
public class UserService {
    private static final Map<String, Key> mapKeyPair;

    static {
        try {
            mapKeyPair = RSACoder.initKey();
        } catch (NoSuchAlgorithmException e) {
            log.error("Error generating:{}", e.getMessage());
            throw new RuntimeException(e);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public Key getKeyPublic() {
        return mapKeyPair.get(RSACoder.PUBLIC_KEY);
    }

    public Key getKeyPrivate() {
        return mapKeyPair.get(RSACoder.PRIVATE_KEY);
    }

    public String getPublicKey() {
        try {
            return RSACoder.getPublicKey(mapKeyPair);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public String getPrivateKey() {
        try {
            return RSACoder.getPrivateKey(mapKeyPair);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


}
